package com.jzqf.harmonydemo.views.slice;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.provider.PageProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class PageSliderAbilitySlice extends AbilitySlice {
    private final String[] tabTitles = {"推荐", "图片", "视频"};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_page_slider);
        initView();
    }

    private void initView() {
        //tablist菜单导航
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_pageSlider_tabList);
        for (String tabTitle : tabTitles) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setText(tabTitle);
            tabList.addTab(tab);
        }
        tabList.selectTabAt(0);
        //pageSlider滑动
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pageSlider);
        DirectionalLayout layoutRecommend = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_recommend, null, false);
        DirectionalLayout layoutImage = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_image, null, false);
        DirectionalLayout layoutVideo = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_video, null, false);
        List<Component> pageList = new ArrayList<>();
        pageList.add(layoutRecommend);
        pageList.add(layoutImage);
        pageList.add(layoutVideo);
        PageProvider pageProvider = new PageProvider(pageList);
        pageSlider.setProvider(pageProvider);
        pageSlider.setCurrentPage(0,true);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                tabList.selectTabAt(i);
            }
        });
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                pageSlider.setCurrentPage(tab.getPosition());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
